package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.DaydayCouponBean;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   玩乐住, 美食街,石子街 公用的请求数据列表 置顶滚动栏
 */
public class GetPlayLIstTop extends BaseCommand {

    private int catid; //当前分类编号
    private int list_type = 1;
    private String mid;
    private String ak;
    private String geotable_id;
    private String location;
    private String radius;
    private String level;
    private String TAG = "GetPlayLIstTop";

    public GetPlayLIstTop(int catid,String level,String mid,String ak,String geotable_id, String location,String radius) {
        this.catid = catid;
        this.ak = ak;
        this.mid = mid;
        this.geotable_id = geotable_id;
        this.level = level;
        this.radius = radius;
        this.location = location;
    }




    @Override
    public String getRequestUrl() {
        return DataCenter.GET_BAIDU_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
//        if(catid!=0){
//            paramters.put("catid", catid  + "");
//        }
        paramters.put("filter","level:"+level + ","+level+"|mid:"+""+mid+","+mid+"");
//        paramters.put("radius",radius);
        paramters.put("location",location);
        paramters.put("ak",ak);
        paramters.put("geotable_id",geotable_id);
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetPlayLIstTop.yes()");

        try {
            Log.d(TAG, result.object.toString());
            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray  jsonArray = jsonObject.optJSONArray("contents");
            ArrayList<PlayBean> targetList = DataCenter.playTopBeansTopList;
            targetList.clear();

            for(int i = 0; i < jsonArray.length(); ++i){ // i从1开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                PlayBean bean = new PlayBean();
                bean.setSize(jsonArray.length()+"");
                bean.setThumb(o.getString("thumb"));
                bean.setStar(o.getString("star"));
                bean.setTitle(o.getString("title"));
                bean.setAddress(o.getString("address"));
                bean.setDistance(o.getString("distance"));
                bean.setWeight(o.getString("weight"));
                bean.setItemid(o.getString("itemid"));
                bean.setCatid(o.getString("catid"));
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_TOP_SUCCESS);
    }
}
